﻿@using Smartstore.Core.Search.Facets
@using Smartstore.Web.Models.Catalog
@using Smartstore.Core.Catalog.Products

@model ProductSummaryModel

@{
    var sourceResult = Model?.SourceResult;
    if (sourceResult == null)
    {
        return;
    }

    var templateProvider = Resolve<IFacetTemplateProvider>();
    var facetGroups = sourceResult.Facets.Select(x => x.Value).OrderBy(x => x.DisplayOrder);
    int i = 0;
}

<zone name="search_filters_before" />

<div class="faceted-search-container">
    <div class="faceted-search" id="faceted-search">

        @* Sorting *@
        <div sm-if="Model.AllowSorting" class="facet-group d-lg-none" data-key="orderby">
            <a class="facet-group-header">
                <span>@T("Categories.OrderBy")</span>
            </a>
            <div class="facet-body" aria-expanded="true">
                <div class="facet-body-inner offcanvas-hstack hstack overflow-x-auto gap-1 small">
                    @foreach (var sort in Model.AvailableSortOptions)
                    {
                        var sortName = (sort.Key == (int)ProductSortingEnum.Relevance ? Model.RelevanceSortOrderName : null) ?? sort.Value;
                        var isSelected = sort.Key == Model.CurrentSortOrder;
                        var className = $"badge badge-pill badge-lg badge-orderby fw" + (isSelected ? "m order-0 badge-info badge-subtle selected" : "n order-1 badge-light");
                        var url = sort.Key > 0
                            // Add/update query
                            ? Display.ModifyQueryString(null, $"?o={sort.Key}")
                            // Remove query
                            : Display.ModifyQueryString(null, null, removeParamName: "o");
                        
                        if (isSelected)
                        {
                            <span class="@className">@sortName</span>
                        }
                        else
                        {
                            <a href="@url" class="@className">@sortName</a>
                        }
                    }
                </div>
            </div>
        </div>

        @foreach (var group in facetGroups)
        {
            i++;
            var selectedFilters = group.SelectedFacets.ToArray();
            var selectedCount = selectedFilters.Length;
            var expanded = i <= 3 || selectedCount > 0;
            var widget = templateProvider.GetTemplateWidget(group);

            <div class="facet-group" data-key="@group.Key">
                <a class="facet-group-header facet-toggle@(!expanded ? " collapsed" : "")@(selectedCount > 0 ? " has-selection" : "")"
                   data-toggle="collapse"
                   href="#facet-body-@(group.Key)"
                   aria-expanded="@expanded.ToString().ToLower()"
                   aria-controls="facet-body-@(group.Key)"
                   attr-data-selections="(selectedCount > 0, selectedCount.ToString())">

                    @(group.Label ?? group.Key)
                    <span sm-if="selectedCount > 0" class="badge badge-info badge-subtle badge-ring badge-counter facet-selection">@selectedCount</span>
                </a>
                <div id="facet-body-@(group.Key)" class="facet-body collapse" attr-class='(expanded, "show")' aria-expanded="@expanded.ToString().ToLower()">
                    @if (widget != null)
                    {
                        @await widget.InvokeAsync(ViewContext)
                    }
                    @if (selectedCount > 0)
                    {
                        <a href="@Url.FacetRemove(selectedFilters)" class="btn-remove-group-filters">
                            @T("Search.Facet.RemoveGroupFilters")
                        </a>
                    }
                </div>
            </div>
        }
    </div>
</div>

<zone name="search_filters_after" />